/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.wise.common.device.info.service.impl;

import com.antlang.dao.AntDao;
import com.wise.common.device.info.domain.GfDevice;
import com.wise.eventserver.outsider.engine.service.InstructionEngineService;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.wise.common.device.info.repository.GfDeviceRepository;
import com.wise.common.device.info.service.GfDeviceService;
import com.wise.common.device.info.service.dto.GfDeviceDto;
import com.wise.common.device.info.service.dto.GfDeviceQueryCriteria;
import com.wise.common.device.info.service.mapstruct.GfDeviceMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author gaocm
* @date 2020-11-04
**/
@Service
@RequiredArgsConstructor
public class GfDeviceServiceImpl implements GfDeviceService {

    private final GfDeviceRepository gfDeviceRepository;
    private final GfDeviceMapper gfDeviceMapper;

    @Resource
    private AntDao outsiderDao;
    @Resource
    private InstructionEngineService instructionEngineService;

    @Override
    public Map<String,Object> queryAll(GfDeviceQueryCriteria criteria, Pageable pageable){
        Page<GfDevice> page = gfDeviceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(gfDeviceMapper::toDto));
    }

    @Override
    public List<GfDeviceDto> queryAll(GfDeviceQueryCriteria criteria){
        return gfDeviceMapper.toDto(gfDeviceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public GfDeviceDto findById(Long id) {
        GfDevice gfDevice = gfDeviceRepository.findById(id).orElseGet(GfDevice::new);
        ValidationUtil.isNull(gfDevice.getId(),"GfDevice","id",id);
        return gfDeviceMapper.toDto(gfDevice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GfDeviceDto create(GfDevice resources) {
        return gfDeviceMapper.toDto(gfDeviceRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(GfDevice resources) {
        GfDevice gfDevice = gfDeviceRepository.findById(resources.getId()).orElseGet(GfDevice::new);
        //GfDevice gfDevice = outsiderDao.
        ValidationUtil.isNull( gfDevice.getId(),"GfDevice","id",resources.getId());
        gfDevice.copy(resources);
        gfDeviceRepository.save(gfDevice);
        instructionEngineService.testAnt(gfDevice);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            gfDeviceRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<GfDeviceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (GfDeviceDto gfDevice : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("设备类型表id", gfDevice.getDeviceTypeId());
            map.put("设备编号", gfDevice.getDeviceNumber());
            map.put("设备状态", gfDevice.getDeviceState());
            map.put("保密等级", gfDevice.getSecretLevel());
            map.put("负责人编号", gfDevice.getManager());
            map.put("设备的IP地址", gfDevice.getDeviceIp());
            map.put("设备SFTP的端口", gfDevice.getSftpPort());
            map.put("SFTP用户名", gfDevice.getSftpUsername());
            map.put("SFTP密码", gfDevice.getSftpPassword());
            map.put("RSA公钥字符串", gfDevice.getSftpRsaPubkey());
            map.put("RSA私钥字符串", gfDevice.getSftpRsaPrikey());
            map.put("更新时间", gfDevice.getUpdateTime());
            map.put("更新人", gfDevice.getUpdatePerson());
            map.put("创建时间", gfDevice.getCreateTime());
            map.put("创建人", gfDevice.getCreater());
            map.put("操作模式", gfDevice.getOperationMode());
            map.put("设备占用时间", gfDevice.getOccupyTimes());
            map.put("设备的端口", gfDevice.getDevicePort());
            map.put("设备的连接协议", gfDevice.getDeviceAgreement());
            map.put("负责人id", gfDevice.getManagerId());
            map.put("设备名称", gfDevice.getDeviceName());
            map.put("wise码", gfDevice.getWisecode());
            map.put("使用单位（部门）", gfDevice.getUserDepart());
            map.put("备注", gfDevice.getRemarks());
            map.put("资金来源", gfDevice.getFundingSource());
            map.put("启用时间", gfDevice.getEnablationTime());
            map.put("设备类别", gfDevice.getTypeMark());
            map.put("刀位数量", gfDevice.getToolsNum());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}